const app = getApp();
var config = app.globalData.config;
const http = require('../../utils/http.js');
Page({

  data: {
    sid_active: 0,
    order_list: [],
    leave_key: '',
    leave_win: false,
    leave_num: 0
  },


  onLoad(options) {
    this.getOrderList();
  },

  getOrderList() {
    var that = this;
    http.httpPost(config.get_snacks_order_list, {
      status: that.sid_active
    }, function(e) {
      that.order_list = e.data;
      that.setData({
        'order_list': e.data
      })

    });
  },
  //选择订单状态
  sid_change(e) {
		let sid = e.currentTarget.dataset.sid;
		this.sid_active = e.currentTarget.dataset.sid;
		this.setData({
			"sid_active": sid
		});
		this.getOrderList();
	},
  toLeave(e){

    this.setData({
      leave_win : true,
      leave_key : e.currentTarget.dataset.key
    })
  },
  cancelLeave(){
    this.setData({
      leave_win : false
    })
  },
  input_leave_num(e){
    this.setData({
      leave_num : e.detail.value
    })
  },
  confirm_leave_num(){
    var that = this;
    let leave_num = this.data.leave_num;
    let order_list = this.data.order_list;
    if(leave_num==''){
      wx.showToast({
        title: '请选择请假人数',
        icon: 'none',
        duration: 1500,
        mask: true
      })
      return;
    }
    if(leave_num>order_list[this.data.leave_key].expect_num){
      wx.showToast({
        title: '请假人数超过最多用餐人数',
        icon: 'none',
        duration: 1500,
        mask: true
      })
      return;
    }

    http.httpPost(config.set_snacks_leave, {
      leave_num : that.data.leave_num,
      rid : order_list[this.data.leave_key].id
    }, function(e) {
      wx.showToast({
        title: e.msg,
        icon: 'none',
        duration: 1500,
        mask: true
      })
      if(e.code==1){
        that.getOrderList();
      }
    });
    this.setData({
      leave_win : false
    })
  }

})